rm(list = ls())
#setwd("~/Dropbox/Energiewende_Project/All_Replication_Files")
setwd("C:/Users/chris/Dropbox/Energiewende_Project/All_Replication_Files")

#creating survey weights 

library(readr)
library(dplyr)
library(anesrake)
library(weights)

data_cleaned_final <- read_csv("data.coal.cleaned.final.csv")


# * Germany
# /* Age: 18-29: 19%
# Age: 30-44: 21%
# Age: 45-64: 35%
# Age: 65+: 25%
# Gender: Male 49%
# Gender: Female 51%
# */


#preparing data for weighting:
data_2 <- data_cleaned_final %>% mutate(agegroup=cut(age, breaks=c(-Inf, 29,44,64, Inf), labels=c(1,2,3,4), include.lowest = FALSE))

wpct(data_2$incomequintiles)

data_2$gender_w <- dplyr::recode(data_2$female, 
                          "0"=1,
                          "1"=2)

data_2$incomequintiles_w <- dplyr::recode(data_2$incomequintiles, 
                                          "quint1"=1,
                                          "quint2"=2, 
                                          "quint3"=3,
                                          "quint4"=4, 
                                          "quint5"=5)


data_2$agegroup <- as.numeric(paste(data_2$agegroup))


gender_w <- c(.49,.51)
agegroup  <- c(0.19, 0.21, 0.35, 0.25)
incomequintiles_w  <-  rep(0.2,5)

targets <- list(gender_w, agegroup, incomequintiles_w)

names(targets) <- c("gender_w", "agegroup","incomequintiles_w")

data_2$caseid <- 1:length(data_2$region)

data_2 <- as.data.frame(data_2)


anesrakefinder(targets, data_2, choosemethod = "total")


outsave <- anesrake(targets, data_2, caseid = data_2$caseid,
                    verbose= FALSE, cap = 5, choosemethod = "total",
                    type = "pctlim", pctlim = .05 , nlim = 5,
                    iterate = TRUE , force1 = TRUE)

data_2$weights <- outsave$weightvec

weights_data <- data_2 %>% select(tic, weights)

write_csv(weights_data, "weights_data.csv")

